package com.hzt.system.controller;

import com.hzt.system.common.Constant;
import com.hzt.system.common.DataGridView;
import com.hzt.system.common.ResultObj;
import com.hzt.system.domain.Dept;
import com.hzt.system.service.DeptService;
import com.hzt.system.vo.DeptVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
*
*
* @Author:hzt
* @Date:8:54 下午 2020/7/15
*/
@RestController
@RequestMapping("api/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("loadAllDept")
    public Object loadAllDept(DeptVo deptVo){
        return deptService.loadAllDept(deptVo);
    }


    //查询最大排序码
    @GetMapping("queryDeptMaxOrderNum")
    public Object queryDeptMaxOrderNum(){
        Integer  maxValue=this.deptService.queryDeptMaxOrderNum();
        return new DataGridView(maxValue+1);
    }


    /**
     * 要放入缓存的
     *  @param dept
     * @return
     */
    @PostMapping("addDept")
    @RequiresPermissions("dept:add") //按钮权限
    public ResultObj addDept(Dept dept){
        try {
            dept.setAvailable(Constant.AVAILABLE_TRUE);
            dept.setSpread(Constant.SPREAD_FALSE);
            this.deptService.saveDept(dept);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    //更新操作
    @RequestMapping("updateDept")
    @RequiresPermissions("dept:update")
    public ResultObj updateDept(Dept dept){
        try {
            this.deptService.updateDept(dept);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    //根据id找对象，主要获取部门title
    @GetMapping("getDeptById")
    @ResponseBody
    public Object getDeptById(Integer id){
        Dept byId = deptService.getById(id);
        System.out.println(byId);
        return new DataGridView(this.deptService.getById(id));
    }


    //查 找当前部门下有无子节点
    @GetMapping("getDeptChildCountById")
    public Object getDeptChildCountById(Integer id){
       Integer count= this.deptService.queryDeptChildCountById(id);
        return new DataGridView(count);
    }


    //删除
    @RequestMapping("deleteDept")
    @RequiresPermissions("dept:delete")
    public ResultObj deleteDept(Dept dept){
        try {
            this.deptService.removeById(dept.getId());
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }



}
